<?php
!defined('IN_HDWIKI') && exit('Access Denied');
class control extends base{

	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('setting');
		$this->view->setlang($this->setting['lang_name'],'back');
	}
	/*base setting*/
	function dobase(){
		if(!isset($this->post['settingsubmit'])){
			$this->view->assign('timeoffset',$this->view->lang['timeoffset']);
			$this->view->assign('basecfginfo',$this->setting);
			$this->view->display("admin_base");
		}else{
			$settings = $this->post['setting'];
			if(!is_numeric($settings['time_diff'])){
				$this->message($this->view->lang['baseConfigMustNum'], 'BACK');
			}
			if(!$settings['cookie_pre']){
				$settings['cookie_pre']='hd_';
			}
			foreach($settings as $key =>$value){
				$settings[$key] = trim($value);
			}
			if('/'==substr($settings['site_url'],-1)){
				$settings['site_url']=substr($settings['site_url'],0,-1);
			}
			$setting=$_ENV['setting']->update_setting($settings);
			$this->cache->removecache('setting');
			$this->message($this->view->lang['baseConfigSuccess'],'index.php?admin_setting-base');
		}
	}
	
	function dobaseregister(){
		if(!isset($this->post['settingsubmit'])){
			$this->view->assign('basecfginfo',$this->setting);
			$this->view->display("admin_baseregister");
		}else{
			$settings = $this->post['setting'];
			foreach($settings as $key =>$value){
				$settings[$key] = trim($value);
			}
			$error_names = $settings['error_names'];
			$error_names = str_replace(",", "\n", $error_names);
			$error_names = str_replace('"', '', $error_names);
			$error_names = explode("\n", $error_names);
			foreach($error_names as $key =>$value){
				$error_names[$key] = trim($value);
				if ('' === $error_names[$key]){unset($error_names[$key]);}
			}
			$settings['error_names'] = implode("\n", $error_names);
			$settings=$_ENV['setting']->update_setting($settings);
			$this->cache->removecache('setting');
			$this->message($this->view->lang['baseConfigSuccess'],'index.php?admin_setting-baseregister');
		}
	}

	/*upload logo*/
	function dologo(){
		if(!isset($this->post['logsubmit'])){
			$this->view->display("admin_uploadlogo");
		}else{
			if(isset($_FILES['logo'])){
				$imgname=$_FILES['logo']['name'];
				$filetype = array('image/jpeg','image/gif','image/x-png','image/png','image/pjpeg');
				if(in_array($_FILES['logo']['type'],$filetype)){
					$destfile='style/default/logo.gif';
					$arrupload=file::uploadfile($_FILES['logo'],$destfile,1024);
				}else{
					$this->message($this->view->lang['uploadFormatWrong'],'BACK');
				}
			}
			if(isset($arrupload) && $arrupload['result'] == true){
				$this->message($arrupload['msg'],'index.php?admin_setting-logo');
			}else{
				$this->message($this->view->lang['uploadFail'],'BACK');
			}
		}
	}

	/*set credit*/
	function docredit(){
		if(!isset($this->post['creditsubmit'])){
			$this->view->assign('creditconfig',$this->setting);
			$this->view->display("admin_credit");
		}else{
			$creditlist=array('credit_register'=>20,'credit_login'=>1,'credit_create'=>5,'credit_edit'=>3,'credit_upload'=>2,'credit_pms'=>1,'credit_comment'=>2);
			foreach($creditlist as $creditkey => $creditvalue){
				$setting[$creditkey]=(intval($this->post[$creditkey])>0)?intval($this->post[$creditkey]):$creditvalue;
			}
			$_ENV['setting']->update_setting($setting);
			$this->cache->removecache('setting');
			$this->message($this->view->lang['bonusSuccess'],'index.php?admin_setting-credit');
		}
	}
	
	function dolistdisplay(){
		if(!isset($this->post['submit'])){
			$this->view->assign('listdisplay',$this->setting);
			$this->view->display("admin_listdisplay");
		}else{
			$listdisplay=array('list_focus','list_recentupdate','list_weekuser','list_allcredit','list_popularity','list_letter','category_view','category_letter','index_commend','index_recentupdate','index_weekuser','index_topuser','index_recentcomment');
			foreach($listdisplay as $display){
				$setting[$display]=(intval($this->post[$display])>0)?intval($this->post[$display]):10;
			}
			$_ENV['setting']->update_setting($setting);
			$this->cache->removecache('setting');
			$this->cache->removecache('indexcache');
			$this->message($this->view->lang['listDisplaySucess'],'index.php?admin_setting-listdisplay');
		}
	}
	
	function doattachment(){
		if(!isset($this->post['attachmentsubmit'])){
 			$this->view->display("admin_attachment");
 		}else{
 			if(!is_numeric($this->post['attachment_size'])){$this->message($this->view->lang['attachmentSizeTrip'],'index.php?admin_setting-attachment');}
 			if($this->post['attachment_type']==""){$this->post['attachment_type']="jpg|jpeg|bmp|gif|zip|rar|doc|ppt|mp3|xls|txt|swf|flv";}
 			unset($this->post['attachmentsubmit']);
 			$_ENV['setting']->update_setting($this->post);
 			$this->cache->removecache('setting');
 			$this->message($this->view->lang['attachmentConfigSucess'],'index.php?admin_setting-attachment');
 		}
	}

	function domail(){
		if(!isset($this->post['mailsubmit'])){
			if(isset($this->setting['mail_config'])){
				$mail_config=unserialize($this->setting['mail_config']);
				$this->view->assign('mailconfig',$mail_config);
			}
 			$this->view->display("admin_mail");
 		}else{
 			$set["mail_host"]=$this->post["mail_host"];
 			$set["mail_port"]=$this->post["mail_port"];
 			$set["mail_mail"]=$this->post["mail_mail"];
 			$set["mail_user"]=$this->post["mail_user"];
 			$set["mail_pass"]=$this->post["mail_pass"];
 			$mail['mail_config']=serialize($set);
 			$setting=$_ENV['setting']->update_setting($mail);
 			$this->cache->removecache('setting');
 			$this->message($this->view->lang['emaliSuc'],'BACK');
 		}
	}

	/*set seo*/
	function doseo(){
		$servertype=strtoupper(substr($_SERVER['SERVER_SOFTWARE'],0,6));
		if(!isset($this->post['seosubmit'])){
			$this->view->assign('servertype',$servertype);
			$this->view->assign('seoconfig',$this->setting);
			$this->view->display("admin_seo");
		}else{
			if(empty($this->post['seo_prefix'])){$this->post['seo_prefix']='index.php?';}
			$seotype['seo_type']=$this->post['seo_type'];
			if($seotype['seo_type']==1){
				if($servertype!='APACHE'){
					$seotype['seo_type']=0;
					$seotype['seo_prefix']=$this->post['seo_prefix'];
					$_ENV['setting']->update_setting($seotype);
					$this->cache->removecache('setting');
					$this->message('Sorry!The server is not apache!','index.php?admin_setting-seo');
				}
				$base_root=substr($_SERVER['PHP_SELF'],0,strpos($_SERVER['PHP_SELF'],"index.php"));
				$data="
<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteBase $base_root
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^.*$ index.php?\$0
</IfModule>";
				$bytes=file::writetofile(HDWIKI_ROOT."/.htaccess",$data);
				if($bytes==0){
					$seotype['seo_type']=0;
					$seotype['seo_prefix']=$this->post['seo_prefix'];
					$_ENV['setting']->update_setting($seotype);
					$this->cache->removecache('setting');
					$this->message($this->view->lang['seoConfigFileWriteError'],'BACK');
				}
				$this->post['seo_prefix']="";
			}else if(file_exists(HDWIKI_ROOT."/.htaccess")){
				unlink (HDWIKI_ROOT."/.htaccess");
			}
			$setting=$_ENV['setting']->update_setting($this->post);
 			$this->cache->removecache('setting');
 			$this->message($this->view->lang['seoConfigSuccess'],'index.php?admin_setting-seo');
		}
	}

	function docache(){
		$arr_cachelist=array(array("variable"=>"index_cache_time","value"=>$this->setting['index_cache_time'],"describ"=>$this->view->lang['delIndeCache'],"cache_name"=>$this->view->lang['indeCache']),
		array("variable"=>"list_cache_time","value"=>$this->setting['list_cache_time'],"describ"=>$this->view->lang['delListCache'],"cache_name"=>$this->view->lang['listCache'])
		);
		$this->view->assign("cachelist",$arr_cachelist);
		$this->view->display("admin_cache");
	}
	
	function doupdatecache(){
		$cachelist = array('index_cache_time','list_cache_time');
		foreach($cachelist as $cache){
			if(!is_numeric($this->post[$cache."_value"])){
				$this->message($this->view->lang['cacheTimeIsNum'],'BACK');
			}
		}
		$_ENV['setting']->update_cache($cachelist,$this->post);
		$this->cache->removecache('setting');
		$this->message($this->view->lang['cacheConfigSuccess'],'index.php?admin_setting-cache');
	}

	function doremovecache(){
		file::cleardir(HDWIKI_ROOT.'/data/cache');
		file::cleardir(HDWIKI_ROOT.'/data/view');
		$this->message($this->view->lang['cacheDelSuccess'],'BACK');
	}
	
	function donotice(){
		if(isset($this->post['notice'])){
			$notice=$this->post['notice'];
			if(empty($notice)){
				$this->load('user');
				$this->load('doc');
				$usernum=$_ENV['user']->get_total_num('',0);
				$docnum=$_ENV['doc']->get_total_num();
				$notice=$this->view->lang['resetnotice'].' '.number_format($usernum).' '.$this->view->lang['resetnotice0'].' '.number_format($docnum).' '.$this->view->lang['resetnotice2'];
			}
			$setting['site_notice']=$notice;
			$_ENV['setting']->update_setting($setting);
			$this->cache->removecache('setting');
			$this->message($this->view->lang['usermanageOptSuccess'],'index.php?admin_setting-notice');
		}
		$this->view->display("admin_notice");
	}
	
	function dopassport(){
		$ppfile=HDWIKI_ROOT.'/data/passport.inc.php';
		if(isset($this->post['passport'])){
			$passportdata="<?php
define('PP_OPEN', '".$this->post['ppopen']."');
define('PP_TYPE', '".$this->post['pptype']."');
define('PP_API', '".$this->post['ppapi']."');
define('PP_NAME', '".$this->post['ppname']."');
define('PP_LOGIN', '".$this->post['pplogin']."');
define('PP_LOGOUT', '".$this->post['pplogout']."');
define('PP_REG', '".$this->post['ppreg']."');
define('PP_KEY', '".$this->post['ppkey']."');
?>";
			$byte=file::writetofile($ppfile,$passportdata);
			if($byte==0){
				$this->message($this->view->lang['passportnotwrite'],'BACK');
			}else{
				$this->message($this->view->lang['passportsucess'],'index.php?admin_setting-passport');
			}
		}else{
			if(file_exists($ppfile)){
				include($ppfile);
				if(defined('PP_API')){
					$this->view->assign('pp_open',PP_OPEN);
					$this->view->assign('pp_type',PP_TYPE);
					$this->view->assign('pp_api',PP_API);
					$this->view->assign('pp_name',PP_NAME);
					$this->view->assign('pp_login',PP_LOGIN);
					$this->view->assign('pp_logout',PP_LOGOUT);
					$this->view->assign('pp_reg',PP_REG);
					$this->view->assign('pp_key',PP_KEY);
				}
			}
			$this->view->display("admin_passport");
		}
	}
}
?>